//
//  HGSign.m
//  HGMM
//
//  Created by @HUI on 15/9/21.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "HGSign.h"
#import "HGPluginViewController.h"
#import "SignedImageView.h"

@implementation HGSign

-(void)getSign:(CDVInvokedUrlCommand *)command{
    
    __block CDVPluginResult *pluginResult = nil;
    if (command.arguments.count !=3 || ![command.arguments[0] isKindOfClass:[NSString class]] || ![command.arguments[1] isKindOfClass:[NSString class]] || !command.arguments[2]) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"args is wrong!"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        return ;
    }
    
    HGPluginViewController *vc;
    if ([self.viewController isKindOfClass:[HGPluginViewController class]]) {
        vc = (HGPluginViewController *)self.viewController;
        dispatch_async(dispatch_get_main_queue(), ^{
            
            SignedImageView *sign = [[SignedImageView alloc]init];
            sign.time = command.arguments[1];
            sign.msg = command.arguments[0];
            sign.lineWidth = [command.arguments[2] floatValue];
            sign.pluginId = Plugin_IN; //vc.pluginId;
            [sign showWithBlock:^(NSString *signPath) {
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:signPath];
                HGLog(@"%@",self);
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            }];
        });
    }
    
}


-(void)getNewSign:(CDVInvokedUrlCommand *)command{
    
    
    __block CDVPluginResult *pluginResult = nil;
    if (command.arguments.count !=5 || ![command.arguments[0] isKindOfClass:[NSString class]] || ![command.arguments[1] isKindOfClass:[NSString class]]) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"args is wrong!"];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        return ;
    }
    
    HGPluginViewController *vc;
    if ([self.viewController isKindOfClass:[HGPluginViewController class]]) {
        vc = (HGPluginViewController *)self.viewController;
        dispatch_async(dispatch_get_main_queue(), ^{
            
            SignedImageView *sign = [[SignedImageView alloc]init];
            sign.time = command.arguments[1];
            sign.msg = command.arguments[0];
            sign.lineWidth = [command.arguments[2] floatValue];
            NSInteger x = [command.arguments[3] integerValue];
            NSInteger y = [command.arguments[4] integerValue];
            
            sign.pluginId = Plugin_IN;
            //            [sign showWithBlock:^(NSString *signPath) {
            //                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:signPath];
            //
            //                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            //            }];
            
            [sign showx:x showy:y WithCallbackBlock:^(NSString *signPath) {
                
                
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:signPath];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                
                
            }];
            
            
        });
    }
    
}

@end
